package com.zy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zy.entity.JsonResult;
import com.zy.entity.Work;
import com.zy.entity.WorkTime;
import com.zy.mapper.WorkMapper;
import com.zy.mapper.WorkTimeMapper;
import com.zy.tools.RedisTool;
import com.zy.tools.appletsTools.TimeTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/app")
public class WorksController {
    @Autowired
    RedisTool redisTool;
    @Autowired
    private WorkMapper workMapper;
    @Autowired
    private WorkTimeMapper workTimeMapper;
    @Autowired
    private TimeTool timeTool;

    /**
     * 上下班打卡
     *
     * @param uid  用户id
     * @param type 打卡类型
     * @return com.zy.entity.JsonResult
     * @author 小宁
     * @date 2021-04-08 17:48
     */
    @ResponseBody
    @GetMapping("/addWork")
    public JsonResult addWork(Integer uid, Integer type) throws ParseException {
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        String time = sf.format(date);
        String sqlDate = time.substring(0, 10);
        Work work = new Work();
        WorkTime workTime = workTimeMapper.selectById(1);//因为规定时间固定一条，所以固定
        if (type == 0) {//上班
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("user_id", uid);
            queryWrapper.eq("date", sqlDate);
            Work work1 = workMapper.selectOne(queryWrapper);
            if (work1 != null) {
                if (!work1.getStartTime().equals("")) {//判断是否已打卡
                    return new JsonResult("200", "上班已打卡", "-1");
                }
            }
            String onWork = sqlDate + " " + workTime.getOnWork();//规定上班时间
            System.out.println("规定上班时间：" + onWork);
            if (timeTool.dateToStampLong(time) - timeTool.dateToStampLong(onWork) > 0) {
                work.setOnWork(1);//上班打卡 迟到
                work.setUserId(uid);
                work.setDate(sqlDate);
                work.setStartTime(time);
                workMapper.insert(work);
                return new JsonResult("200", "上班打卡,迟到", "1");
            } else {
                work.setOnWork(0); //上班打卡 正常
                work.setUserId(uid);
                work.setDate(sqlDate);
                work.setStartTime(time);
                workMapper.insert(work);
                return new JsonResult("200", "上班打卡,正常", "0");
            }
        } else {//下班
            QueryWrapper queryWrapper0 = new QueryWrapper();
            queryWrapper0.eq("user_id", uid);
            queryWrapper0.eq("date", sqlDate);
            Work work1 = workMapper.selectOne(queryWrapper0);
            if (work1 != null) {//有上班记录
                if (!work1.getEndTime().equals("")) {//判断是否已打卡
                    return new JsonResult("200", "下班已打卡", "-1");
                }
                String onWork = sqlDate + " " + workTime.getOffWork();//规定下班时间
                QueryWrapper queryWrapper = new QueryWrapper();
                if (timeTool.dateToStampLong(time) - timeTool.dateToStampLong(onWork) > 0) {
                    work.setOffWork(0);//下班打卡正常
                    work.setEndTime(time);
                    queryWrapper.eq("user_id", uid);
                    queryWrapper.eq("date", sqlDate);
                    workMapper.update(work, queryWrapper);
                    return new JsonResult("200", "下班打卡,正常", "0");
                } else {
                    work.setOffWork(1);//下班打卡 早退
                    work.setEndTime(time);
                    queryWrapper.eq("user_id", uid);
                    queryWrapper.eq("date", sqlDate);
                    workMapper.update(work, queryWrapper);
                    return new JsonResult("200", "下班打卡,早退", "1");
                }
            } else {//没上班记录
                String onWork = sqlDate + " " + workTime.getOffWork();//规定下班时间
                QueryWrapper queryWrapper = new QueryWrapper();
                if (timeTool.dateToStampLong(time) - timeTool.dateToStampLong(onWork) > 0) {
                    work.setOffWork(0);//下班打卡正常
                    work.setEndTime(time);
                    work.setUserId(uid);
                    work.setDate(sqlDate);
                    workMapper.insert(work);
                    return new JsonResult("200", "下班打卡,正常", "0");
                } else {
                    work.setOffWork(1);//下班打卡 早退
                    work.setEndTime(time);
                    work.setUserId(uid);
                    work.setDate(sqlDate);
                    workMapper.insert(work);
                    return new JsonResult("200", "下班打卡,早退", "1");
                }
            }

        }
    }


    /**
     * 根据用户id查询用户30天的考勤记录
     *
     * @param uid
     * @return com.zy.entity.JsonResult
     * @author 小宁
     * @date 2021-04-05 16:04
     */
    @ResponseBody
    @GetMapping("/getWorkList")
    public JsonResult getWorkList(Integer uid) {
        List<Work> workList = null;

        if (redisTool.hasKey("workList")) {
            redisTool.get("workList");
        } else {
            workList = workMapper.getWorkList(uid);
            redisTool.set("workList", workList, 60 * 60 * 24);
        }
        return new JsonResult("200", "", workList);
    }

    /**
     * 查询上下班时间
     *
     * @return com.zy.entity.JsonResult
     * @author 小宁
     * @date 2021-04-09 9:37
     */
    @ResponseBody
    @GetMapping("/getWorkTime")
    public JsonResult getWorkTime() {
        WorkTime workTime = workTimeMapper.selectById(1);
        return new JsonResult("200", "", workTime);
    }
}
